<?php
class AdvertisementsController extends AppController {

	var $name = 'Advertisements';
	
	function beforeFilter(){
		if($this->Session->read('Auth.User')){
			$this->Auth->allow('*');
		}
	}

	function admin_index() {
		$this->Advertisement->recursive = 0;
		$this->set('advertisements', $this->paginate());
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Advertisement->create();
			if ($this->Advertisement->save($this->data)) {
				$this->Session->setFlash(__('The advertisement has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The advertisement could not be saved. Please, try again.', true));
			}
		}
		$categories = $this->Advertisement->Category->find('list');
		$this->set(compact('categories'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid advertisement', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Advertisement->save($this->data)) {
				$this->Session->setFlash(__('The advertisement has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The advertisement could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Advertisement->read(null, $id);
		}
		$categories = $this->Advertisement->Category->find('list');
		$this->set(compact('categories'));
	}

	function admin_delete($id = null) {
		if(($this->RequestHandler->isAjax())) {
			$this->autoRender = false;
			if (!$id) {
				return 'error';
			}
			$banner = $this->Advertisement->read(null, $id);
			if ($this->Advertisement->delete($id)) {
				$filePath = 'img/files/';
				if(unlink($filePath.$banner['Advertisement']['image'])){
					return 'Success';
				}else{
					return 'error';
				}
			}
			return 'error';
		}
	}
	
	function admin_save(){
		if (!empty($this->data) && isset($this->data['Advertisement']['File'])) {
			$filePath = 'img/files/';
			if( $this->validate() == 1){
				if (file_exists($filePath . $this->data['Advertisement']['File']['name'])){
					$i = 0;
					while(file_exists($filePath .$i++. $this->data['Advertisement']['File']['name']));
					$this->data['Advertisement']['File']['name'] = $i-1 . $this->data['Advertisement']['File']['name'];
				}
				if(move_uploaded_file($this->data['Advertisement']['File']['tmp_name'],$filePath . $this->data['Advertisement']['File']['name'])){
					$this->data['Advertisement']['image'] = $this->data['Advertisement']['File']['name'];
					$this->data['Advertisement']['size'] = $this->data['Advertisement']['File']['size'];
					unset($this->data['Advertisement']['File']);
					$this->Advertisement->create();
					if ($this->Advertisement->save($this->data)) {
						$this->redirect(array('action' => 'index'));
					} else {
						$this->Session->setFlash(__('The banner could not be saved. Please, try again.', true));
					}
				}else{
					$this->Session->setFlash(__('The banner could not be saved. Please, try again.', true));
				}
			}
		}else{
			$this->Advertisement->create();
			if ($this->Advertisement->save($this->data)) {
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The banner could not be saved. Please, try again.', true));
			}
		}
	}
	
	function validate(){
		$extension = $this->getFileExtension($this->data['Advertisement']['File']['name']);
		if (( 
			($this->data['Advertisement']['File']['type'] == "image/gif") ||
			($this->data['Advertisement']['File']['type'] == "image/jpeg")	|| 
			($this->data['Advertisement']['File']['type'] == "image/pjpeg") ||
			($this->data['Advertisement']['File']['type'] == "image/png") 
			)){
			if ($this->data['Advertisement']['File']['error'] == 0){
					return 1;					
			}else{
				return -1;
			}
		}else{
			return 0;
		}
	}
	
	function getAds($category = NULL){
		if($category != NULL && $category != ''){
			$this->loadModel('Category');
			$categoryId = $this->Category->find('list', array('conditions' => array(
														'Category.alias' => $category),
												'fields' => array(
														'id', 'id')));
			$bannerId = $this->Category->AdvertisementsCategory->find('list', array('conditions' => array(
																					'AdvertisementsCategory.category_id' => $categoryId),
																			  'fields' => array(
																					'id', 'advertisement_id')));
			$banners = $this->Advertisement->find('list', array('fields' => array(
																'id','image'),
														 'conditions' => array(
																'Advertisement.id' => $bannerId)));
		}else{
			$banners = $this->Advertisement->find('list', array('fields' => array('id','image')));
		}
		return (!empty($banners))?($banners):'';
	}
	
}
?>